package ad.android.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import ad.android.dao.IAdAppStatisticsDao;
import ad.android.dao.base.BaseDao;
import ad.android.exception.DaoException;
import ad.android.model.AdApplicationStatistics;

public class AdAppStatisticsDaoHibernateImpl extends BaseDao implements
		IAdAppStatisticsDao {

	@Override
	public void saveOrUpdate(AdApplicationStatistics adAppStatistics)
			throws DaoException {
		try {
			if( adAppStatistics == null){
				log.error("Parameter adAppStatistics is null.");
				throw new DaoException("Parameter adAppStatistics is null.");
			}

			super.saveOrUpdate(adAppStatistics);
		} catch (Exception e) {
			log.error("saveOrUpdate",e);
			throw new DaoException(e);
		}
		
	}

	@Override
	public AdApplicationStatistics findAdAppStatistics(Long appId, Long adId)
			throws DaoException {
		try {
			Session session = getSession();
			
			String hql = "from AdApplicationStatistics adapps where adapps.tblApplication.id=:appId and adapps.tblAd.id=:adId";
			Query query = session.createQuery(hql);
			query.setLong("appId", appId);
			query.setLong("adId", adId);
			
			Object obj = query.uniqueResult();
			
			if( obj == null){
				return null;
			}
			
			return (AdApplicationStatistics)obj;
		} catch (HibernateException e) {
			log.error("findAdAppStatistics",e);
			throw new DaoException(e);
		}
	}
	
}
